package com.mall.shopping.services;

import com.mall.shopping.IContentService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.dal.entitys.PanelContent;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.NavListResponse;
import com.mall.shopping.dto.PanelContentDto;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Component
public class ContentService implements IContentService {

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ContentConverter contentConverter;

    /**
     * 获取导航栏信息
     * @return
     */
    @Override
    public NavListResponse queryNavList() {
        NavListResponse response = new NavListResponse();
        //导航栏的商品panelId等于0
        Example example = new Example(PanelContent.class);
        example.createCriteria().andEqualTo("panelId", 0);
        List<PanelContent> panelContents = null;
        try {
            panelContents = panelContentMapper.selectByExample(example);
        } catch (Exception e) {
            response.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            response.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
            e.printStackTrace();
        }
        List<PanelContentDto> panelContentDtos = contentConverter.panelContents2Dto(panelContents);
        response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        response.setCode(ShoppingRetCode.SUCCESS.getCode());
        response.setPannelContentDtos(panelContentDtos);
        return response;
    }
}
